/*
 * @Author: llch
 * @Date: 2020-06-04 09:33:34
 * @LastEditTime: 2020-06-04 16:51:09
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \idiom_demo\assets\script\game\Res.ts
 */ 

import { Word } from "./Word"

const {ccclass, property} = cc._decorator;

@ccclass
export class Res extends cc.Component {

    @property(cc.Node)
    public win_node: cc.Node = null;
    @property(cc.Label)
    public level_label: cc.Label = null;
    @property(cc.Node)
    public wordList_node: cc.Node = null;
    @property(cc.Prefab)
    public wordPre: cc.Node = null;

    @property(cc.Node)
    public wordPanel_node: cc.Node = null;
    @property(cc.Label)
    public chars_label: cc.Label = null;
    @property(cc.Label)
    public pinyin_label: cc.Label = null;
    @property(cc.Label)
    public node_content: cc.Label = null;
    @property(cc.Label)
    public source_content: cc.Label = null;
    @property(cc.Node)
    public sourceNode: cc.Node = null;
    
    // 音效资源
    @property({
        type: cc.AudioClip,
    })
    public chooseAudioClicp: cc.AudioClip = null;
    @property({
        type: cc.AudioClip,
    })
    public clickAudioClicp: cc.AudioClip = null;
    @property({
        type: cc.AudioClip,
    })
    public nextAudioClicp: cc.AudioClip = null;
    @property({
        type: cc.AudioClip,
    })
    public wowAudioClicp: cc.AudioClip = null;
    @property({
        type: cc.AudioClip,
    })
    public rightAudioClicp: cc.AudioClip = null;

    public showWin(idiomsArr, currentLevel: number) {
        this.wordList_node.removeAllChildren();
        
        for (let i = 0; i < idiomsArr.length; i++) {
            let wordNode = cc.instantiate(this.wordPre);
            wordNode.parent = this.wordList_node;
            let wordCom = wordNode.getComponent(Word);
            wordCom.wordLabel.string = idiomsArr[i].data.chars;
            wordCom.data = idiomsArr[i].data;

            this.wordAddListener(wordNode, this.onWordClick, this);
        }
        this.level_label.string = "第 " + currentLevel.toString() + " 关";
        this.win_node.active = true;
    }

    // 成语添加 点击事件
    private wordAddListener(node:cc.Node, fn: Function, tg) {
        node.on(cc.Node.EventType.TOUCH_END, fn, tg);
    }

    // 成语 点击回调
    public onWordClick(e) {
        // 播放音效
        cc.audioEngine.play(this.clickAudioClicp, false, 1);

        let wordCom: Word = e.target.getComponent(Word);
        let data = wordCom.data;
        this.chars_label.string = data.chars;
        this.pinyin_label.string = data.pinyin;
        
        let arr = [];
        arr = data.note.split("出处：");
        if (arr.length === 2) {
            this.sourceNode.active = true;
            this.node_content.string = "  " + arr[0];
            this.source_content.string = "  " + arr[1];
        } else {
            this.sourceNode.active = false;
            this.node_content.string = "  " + arr[0];
            this.source_content.string = "";
        }
        
        this.wordPanel_node.active = true;
        this.wordPanel_node.getComponent(cc.Animation).play("showWord");
    }

    public onCloseBtnClick() {
        // 播放音效
        cc.audioEngine.play(this.chooseAudioClicp, false, 1);

        this.wordPanel_node.active = false;
    }

    // 隐藏
    public hideWin() {
        this.win_node.active = false;
        this.wordPanel_node.active = false;
    }
}